/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * MenuView.java
 *
 * Created on 3 janv. 2013, 18:11:40
 */
package polybook.view;

import java.awt.event.KeyEvent;
import java.io.IOException;
import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import polybook.MainController;
import polybook.model.Network;
import polybook.model.NetworkPersonLinked;
import polybook.model.NetworkTabLinked;
import polybook.model.Person;
import polybook.utils.NetworkThread;

/**
 *
 * @author Arnaud
 */
public class MenuView extends NamedJPanel {

    protected static final String title = "Gestion du réseau";

    /**
     * Creates new form MenuView
     */
    public MenuView() {
        initComponents();

        // Ajout des actions sur les boutons.

    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        btnCreateNetwork = new javax.swing.JButton();
        networkSizeTextField = new javax.swing.JTextField();
        inputAddPerson = new javax.swing.JTextField();
        btnAddMembersSlow = new javax.swing.JButton();
        networkSizeSlowTextField = new javax.swing.JTextField();
        btnCreateNetworkSlow = new javax.swing.JButton();
        quickMethodeLabel = new javax.swing.JLabel();
        slowMethodeLabel1 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTextPane1 = new javax.swing.JTextPane();
        jScrollPane2 = new javax.swing.JScrollPane();
        jTextPane2 = new javax.swing.JTextPane();
        jLabel1 = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();

        setBorder(javax.swing.BorderFactory.createTitledBorder(getTitle()));
        setPreferredSize(new java.awt.Dimension(250, 350));

        btnCreateNetwork.setText("Générer le noyau");
        btnCreateNetwork.setToolTipText("Choose the size and create your own network.");
        btnCreateNetwork.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generationReseauRapide(evt);
            }
        });

        networkSizeTextField.setToolTipText("Select the size of your network");
        networkSizeTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                networkSizeTextFieldKeyTyped(evt);
            }
        });

        inputAddPerson.setToolTipText("Select how many people you want to add ");
        inputAddPerson.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                inputAddPersonKeyTyped(evt);
            }
        });

        btnAddMembersSlow.setText("Ajouter des membres");
        btnAddMembersSlow.setToolTipText("Choose how many people you want to add to your network");
        btnAddMembersSlow.setActionCommand("");
        btnAddMembersSlow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddMembersSlowActionPerformed(evt);
            }
        });

        networkSizeSlowTextField.setToolTipText("Select the size of your network");
        networkSizeSlowTextField.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                networkSizeSlowTextFieldKeyTyped(evt);
            }
        });

        btnCreateNetworkSlow.setText("Générer le noyau");
        btnCreateNetworkSlow.setToolTipText("Choose the size and create your own network.");
        btnCreateNetworkSlow.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                generationReseauLente(evt);
            }
        });

        quickMethodeLabel.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        quickMethodeLabel.setText("Créer un réseau - Implémentation 1");

        slowMethodeLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        slowMethodeLabel1.setText("Créer un réseau - Implémentation 2");

        jScrollPane1.setBorder(null);

        jTextPane1.setBorder(null);
        jTextPane1.setEditable(false);
        jTextPane1.setText("Cette implémentation gère les liens d'amitiés dans un tableaux contenant n instance d'une personne pour n nombre d'amis de cette personne (moins rapide). ");
        jTextPane1.setEnabled(false);
        jTextPane1.setFocusable(false);
        jTextPane1.setOpaque(false);
        jTextPane1.setPreferredSize(new java.awt.Dimension(680, 56));
        jScrollPane1.setViewportView(jTextPane1);

        jScrollPane2.setBorder(null);

        jTextPane2.setBorder(null);
        jTextPane2.setEditable(false);
        jTextPane2.setText("Cette implémentation génère les liens d'amitiés à partir du calcul du degrés du réseau par rapport au degré de chaque personne qu'il contient. ");
        jTextPane2.setEnabled(false);
        jTextPane2.setFocusable(false);
        jTextPane2.setPreferredSize(new java.awt.Dimension(682, 56));
        jScrollPane2.setViewportView(jTextPane2);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 11)); // NOI18N
        jLabel1.setText("Insérer de nouvelles personnes");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1)
                    .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(slowMethodeLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 0, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(networkSizeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnCreateNetwork, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(networkSizeSlowTextField, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnCreateNetworkSlow, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(quickMethodeLabel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(inputAddPerson, javax.swing.GroupLayout.PREFERRED_SIZE, 79, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(btnAddMembersSlow, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(quickMethodeLabel)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(networkSizeTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCreateNetwork))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(slowMethodeLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(networkSizeSlowTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnCreateNetworkSlow))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(inputAddPerson, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btnAddMembersSlow))
                .addContainerGap(37, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void generationReseauRapide(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generationReseauRapide

        //Controller
        MainController controller = MainController.getInstance();
        
        try {
                //Nombre de personne noyau
                Integer numberPeople = Integer.parseInt(this.networkSizeTextField.getText());
                
                if (numberPeople != null && numberPeople >= 0) {
            

                //Initialisation d'un réseau rapide avec la valeur du noyau saisie
                NetworkTabLinked networkTab = new NetworkTabLinked(numberPeople);
                //Assignement du nouveau réseau courant
                controller.setCurrentNetwork(networkTab);

                //Refresh de la JTable
                ArrayList<Person> personList = networkTab.getContainer();
                PersonJTableModel currentDatasModel = new PersonJTableModel(personList);
                controller.getCurrentTable().setModel(currentDatasModel);
                
                // MAJ du bloc d'info reseau
                InfosDefaultView infosView = new InfosDefaultView(networkTab);
                infosView.includeIn(controller.getBaseView().getInfosPanel());
        }
            } catch (IOException ex) {
                Logger.getLogger(MenuView.class.getName()).log(Level.SEVERE, null, ex);
            } catch (IllegalAccessException ex) {
                Logger.getLogger(MenuView.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(controller, "Veuillez saisir la taille du réseau à générer.", "Erreur rencontrée", JOptionPane.ERROR_MESSAGE);
            }
            
        
            //On actualise la barre d'information à la fin du chargement
            // Creation de la vue InfosDefaultView
            InfosDefaultView infosView = new InfosDefaultView(controller.getCurrentNetwork());
            infosView.includeIn(controller.getBaseView().getInfosPanel());
        
            this.networkSizeTextField.setText("");
    }//GEN-LAST:event_generationReseauRapide

    private void generationReseauLente(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_generationReseauLente

        //Controller
        MainController controller = MainController.getInstance();
        
        try {
            //Nombre de personne noyau
            Integer numberPeople = Integer.parseInt(this.networkSizeSlowTextField.getText());

            if (numberPeople != null && numberPeople >= 0) {

                    //Initialisation d'un réseau rapide avec la valeur du noyau saisie
                    NetworkPersonLinked networkTab = new NetworkPersonLinked(numberPeople);
                    //Assignement du nouveau réseau courant
                    controller.setCurrentNetwork(networkTab);

                    //Refresh de la JTable
                    ArrayList<Person> personList = networkTab.getContainer();
                    PersonJTableModel currentDatasModel = new PersonJTableModel(personList);
                    controller.getCurrentTable().setModel(currentDatasModel);

                    // MAJ du bloc d'info reseau
                    InfosDefaultView infosView = new InfosDefaultView(networkTab);
                    infosView.includeIn(controller.getBaseView().getInfosPanel());
            }

            } catch (IOException ex) {
                Logger.getLogger(MenuView.class.getName()).log(Level.SEVERE, null, ex);
            } catch (NumberFormatException ex) {
                JOptionPane.showMessageDialog(controller, "Veuillez saisir la taille du réseau à générer.", "Erreur rencontrée", JOptionPane.ERROR_MESSAGE);
            }
        
        
            //On actualise la barre d'information à la fin du chargement
            // Creation de la vue InfosDefaultView
            InfosDefaultView infosView = new InfosDefaultView(controller.getCurrentNetwork());
            infosView.includeIn(controller.getBaseView().getInfosPanel());
        
        this.networkSizeSlowTextField.setText("");
    }//GEN-LAST:event_generationReseauLente

    private void btnAddMembersSlowActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddMembersSlowActionPerformed
        // Controller
        MainController controller = MainController.getInstance();
        
        //Contrôle de la taille du réseau initial qui ne doit pas être nul
        if(controller.getCurrentNetwork().size()==0)
        {
            JOptionPane.showMessageDialog(controller, "Le réseau initial ne doit pas être vide.", "Erreur rencontrée", JOptionPane.ERROR_MESSAGE);
        }
        else {
            try {
                // Nombre de personne à ajouter
                Integer numberPeople = Integer.parseInt(this.inputAddPerson.getText());
                controller.lockWindow();

                if (numberPeople != null && numberPeople > 0) {
                    NetworkThread thread = new NetworkThread(numberPeople);
                    thread.start();
                }
            } catch (NumberFormatException ex) {
                    JOptionPane.showMessageDialog(controller, "Veuillez saisir le nombre de personne à ajouter au réseau.", "Erreur rencontrée", JOptionPane.ERROR_MESSAGE);
                }
        }
        
    }//GEN-LAST:event_btnAddMembersSlowActionPerformed

    private void networkSizeTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_networkSizeTextFieldKeyTyped
        checkIntValue(evt);
    }//GEN-LAST:event_networkSizeTextFieldKeyTyped

    private void networkSizeSlowTextFieldKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_networkSizeSlowTextFieldKeyTyped
        checkIntValue(evt);
    }//GEN-LAST:event_networkSizeSlowTextFieldKeyTyped

    private void inputAddPersonKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_inputAddPersonKeyTyped
        checkIntValue(evt);
    }//GEN-LAST:event_inputAddPersonKeyTyped

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddMembersSlow;
    private javax.swing.JButton btnCreateNetwork;
    private javax.swing.JButton btnCreateNetworkSlow;
    private javax.swing.JTextField inputAddPerson;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JTextPane jTextPane1;
    private javax.swing.JTextPane jTextPane2;
    private javax.swing.JTextField networkSizeSlowTextField;
    private javax.swing.JTextField networkSizeTextField;
    private javax.swing.JLabel quickMethodeLabel;
    private javax.swing.JLabel slowMethodeLabel1;
    // End of variables declaration//GEN-END:variables

    @Override
    public String getTitle() {
        return title;
    }

    private void checkIntValue(KeyEvent evt) {
        char caractereSaisi = evt.getKeyChar();
        String caractereSaisiString = ""+caractereSaisi;
        try {
            Integer nbSaisi = Integer.parseInt(caractereSaisiString);
        }
        catch (NumberFormatException ex) {
            evt.consume();
        }
    }    
}
